var jCategoryTree;



//点击树节点action
function clickTreeItemAction(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	if(treeItem){
		editRegionForm(treeItem.id);
	}
	
}

//根据树节点显示按钮
function displayButtons(regionId){
	if(regionId == null){
		//如果没有什么节点
		disableIconBtn("moveUp");
		disableIconBtn("moveDown");
		disableIconBtn("moveTop");
		disableIconBtn("moveBottom");
		disableIconBtn("add");
		disableIconBtn("delete");
		disableIconBtn("save");
	}else if(regionId == ""){
		//如果是新增节点则只开发保存按钮
		disableIconBtn("moveUp");
		disableIconBtn("moveDown");
		disableIconBtn("moveTop");
		disableIconBtn("moveBottom");
		disableIconBtn("add");
		disableIconBtn("delete");
		enableIconBtn("save");
	}else if(regionId == "1" || regionId=="78254"){
		//如果是根节点则屏蔽删除按钮
		disableIconBtn("moveUp");
		disableIconBtn("moveDown");
		disableIconBtn("moveTop");
		disableIconBtn("moveBottom");
		enableIconBtn ("add");
		disableIconBtn("delete");
		enableIconBtn ("save");
	}else{
		//开放全部
		enableIconBtn ("moveUp");
		enableIconBtn ("moveDown");
		enableIconBtn("moveTop");
		enableIconBtn("moveBottom");
		enableIconBtn ("add");
		enableIconBtn ("delete");
		enableIconBtn ("save");
	}

}

//加载编辑form
function editRegionForm(regionId,callback){
	displayButtons();//先将所有按钮disable	
	showRegionForm(__ctxPath+"/system/region/blank.html?doAction=edit&regionId="+regionId);
}
//新增
function createRegion(){
	displayButtons();//先将所有按钮disable
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	if(treeItem){
		showRegionForm(__ctxPath+"/system/region/blank.html?doAction=edit&parentRegionId="+treeItem.id);
	}
}

//在本页以ajaxPage方式打开Form 
function showRegionForm(url,callback){
	$j("#regionFormDiv").loading().load(url,null,callback);
}

//将地区置前
function upRegion(){
	var jNode = jCategoryTree.getSelected();
	if(jNode.length > 0 && jNode.prevAll("[id]").size()!=0){
		moveActiveCategoryUp(jCategoryTree);
		updateRegionsSortOrder(jNode);
	}else{
		//已经是第一个元素，不需要再置前
	}
	
}
//将地区置前到最顶
function moveTop(){
	var jNode = jCategoryTree.getSelected();
	if(jNode.length > 0 && jNode.prevAll("[id]").size()!=0){
		moveActiveCategoryTop(jCategoryTree);
		updateRegionsSortOrder(jNode);
	}else{
		//已经是第一个元素，不需要再置前
	}
	
}
//将地区放后
function downRegion(){
	var jNode = jCategoryTree.getSelected();
	if(jNode.length > 0  && jNode.nextAll("[id]").size()!=0){
		moveActiveCategoryDown(jCategoryTree);
		updateRegionsSortOrder(jNode);
	}else{
		//已经是最后一个元素，不需要再放后
	}
}

//将地区置到最底部
function moveBottom(){
	var jNode = jCategoryTree.getSelected();
	if(jNode.length > 0 && jNode.nextAll("[id]").size()!=0){
		moveActiveCategoryBottom(jCategoryTree);
		updateRegionsSortOrder(jNode);
	}else{
		//已经是最后一个元素，不需要再放后
	}
	
}

//更新目录排序
function updateRegionsSortOrder(jNode){
	var categoires=jNode.parent("ul").children("li[id]");
	var paramData="";
	for(var i=0;i<categoires.length;i++){
		var id=categoires.get(i).id;
		paramData+="regionIds="+id+"&";
	}
	$j.post(__ctxPath+"/system/region/blank.html?doAction=updateRegionsSortOrder",paramData);
}

//删除目录
function delRegion(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	if(treeItem&&confirm(__FMT.common_message_confirmDeleteThis+treeItem.name+"?")){
		var paramData={regionId:treeItem.id};
		$j.post(__ctxPath+"/system/region/blank.html?doAction=delete",paramData,delRegionCallback,"json");
	}
}
//删除目录callback
function delRegionCallback(data){
	sysMsg(data.message,(data.result!=1));
	if(data.result==1){
		//fill div
		deleteTreeItem(jCategoryTree);
		displayButtons();
		//start:显示父目录
		activeTreeItem(jCategoryTree,data.parentId);
		editRegionForm(""+data.parentId);
		//end:显示父目录
	}
}
